import 'package:flutter/material.dart';
import 'package:progress_indicators/progress_indicators.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// Magma 加载组件
class MagmaLoading {
  static OverlayEntry? _overlayEntry;

  /// 显示加载提示
  static void show(BuildContext context, {String? message}) {
    // 如果已经有加载提示在显示，先隐藏
    hide();

    _overlayEntry = OverlayEntry(
      builder:
          (context) => Material(
            color: Colors.black.withOpacity(0.5),
            child: Center(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 40.w),
                child: Container(
                  padding: EdgeInsets.all(24.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.1),
                        blurRadius: 10,
                        offset: const Offset(0, 4),
                      ),
                    ],
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      JumpingDotsProgressIndicator(
                        fontSize: 40.sp,
                        color: Colors.blue,
                      ),
                      if (message != null) ...[
                        SizedBox(height: 16.h),
                        Text(
                          message,
                          style: TextStyle(
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w500,
                            color: Colors.black87,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ],
                    ],
                  ),
                ),
              ),
            ),
          ),
    );

    Overlay.of(context).insert(_overlayEntry!);
  }

  /// 隐藏加载提示
  static void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }
}
